package com.wanmait.deliverymangementsystem.service.impl;

import com.wanmait.deliverymangementsystem.mapper.UsersMapper;
import com.wanmait.deliverymangementsystem.pojo.Users;
import com.wanmait.deliverymangementsystem.service.UsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class UsersServiceImpl implements UsersService {
    @Resource
    private UsersMapper usersMapper;
    @Override
    public List<Users> findAll() {
        return usersMapper.findAll();
    }

    @Override
    public void userRegister(Users users) {
        usersMapper.insert(users);
    }

    @Override
    public Users userLogin(Users users) {

        return usersMapper.selectByPhone_numberAndPassword(users);
    }

    @Override
    public void charge(Users users) {
        usersMapper.updateWalletByID(users);
    }

    @Override
    public void lastlogintime(Integer id) {
        usersMapper.updateLasLoginTimeByID(id);
    }

    @Override
    public Users findUser(Integer id) {
        return usersMapper.findByID(id);
    }

    @Override
    public Integer userNumber() {
        return usersMapper.findUserNumber();
    }

    @Override
    public void banUser(Integer id, Boolean status) {
        usersMapper.updateEnableById(id,status);
    }

    @Override
    public void updateByID(Users users) {
        usersMapper.updateBYID(users);
    }

    @Override
    public List<Users> search(String name, String phone,String email) {
        System.out.println("name = " + name + ", phone = " + phone + ", email = " + email);
        List<Users> userList=null;
        if (name.isBlank()&&phone.isBlank()&&email.isBlank()) {
            return null;
        }
    else{
            if (!name.isBlank()&& !phone.isBlank()  && email.isBlank())
                userList = usersMapper.findByUserNameAndPhone(name, phone);
            if (!name.isBlank() && phone.isBlank() && email.isBlank())
                userList = usersMapper.findByUserName(name);
            if (name.isBlank() && !phone.isBlank() && email.isBlank())
                userList = usersMapper.findByPhoneNumber(phone);
            if (name.isBlank() && phone.isBlank() && !email.isBlank())
                userList = usersMapper.findByEmail(email);
            if (name.isBlank() && !phone.isBlank() && !email.isBlank())
                userList = usersMapper.findByEmailAndPhone(email, phone);
            if (!name.isBlank() && phone.isBlank() && !email.isBlank())
                userList = usersMapper.findByUserNameAndEmail(email, name);
            if (!name.isBlank() && !phone.isBlank() && !email.isBlank())
                userList = usersMapper.findByEmailAndPhoneAndUserName(name,phone,email);

        }

        return userList;
    }

}
